<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="基础工资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="basePay">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.basePay" placeholder="请输入基础工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="年功工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seniorityWage">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.seniorityWage" placeholder="请输入年功工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="等级工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradedWages">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.gradedWages" placeholder="请输入等级工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="预发绩效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="advancePerformance">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.advancePerformance" placeholder="请输入预发绩效" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="执业金（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executiveFund">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.executiveFund" placeholder="请输入执业金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="报告审核费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportApplicationFee">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.reportApplicationFee" placeholder="请输入报告审核费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="项目绩效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectPerformance">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.projectPerformance" placeholder="请输入项目绩效" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="前期已结算" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="settledThePreviousPeriod">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"   v-model="model.settledThePreviousPeriod" placeholder="请输入前期已结算" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="其它" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.other" placeholder="请输入其它" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="缺勤扣款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="absenceDeduction">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.absenceDeduction" placeholder="请输入缺勤扣款" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="其他扣款（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherDeductions">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.otherDeductions" placeholder="请输入其他扣款" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="应发工资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="grossPay">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.grossPay"  style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item :label="`${year}年累计收入`" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="annualCumulativeIncome">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.annualCumulativeIncome" :placeholder="`请输入${year}年累计收入`" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="下月社保" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insuranceForNextMonth">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.insuranceForNextMonth" placeholder="请输入下月社保" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="当月公积金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="monthBond">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.monthBond" placeholder="请输入当月公积金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item :label="year + `年五险一金累计扣款`" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yearFiveInsurancesCount">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.yearFiveInsurancesCount" :placeholder="`请输入` + year + `年五险一金累计扣款`" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item :label="year + `年应预缴个人所得税`" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yearAdvanceCount">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.yearAdvanceCount" :placeholder="`请输入` + year + `年应预缴个人所得税`" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item :label="year + `年已预缴个人所得税金额`" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yearPaidIndividualCount">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.yearPaidIndividualCount" :placeholder="`请输入` + year + `年已预缴个人所得税金额`" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="本期应预缴个人所得税" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentDuePersonal">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.currentDuePersonal" placeholder="请输入本期应预缴个人所得税" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item :label="year + `年累计个税专项附加扣除金额`" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yearSpecialCount">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"   v-model="model.yearSpecialCount" :placeholder="`请输入` + year + `年累计个税专项附加扣除金额`" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="前期已发放" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="previouslyIssued">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"  @change="inputAmount"  v-model="model.previouslyIssued" placeholder="请输入前期已发放" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="实发工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netPayment">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" @change="inputAmount"  v-model="model.netPayment"  style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ErpPayrollManagementForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        year: new Date().getFullYear(),
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        basePay: [
            { required: true, message: '请输入基础工资!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        seniorityWage: [
            { required: true, message: '请输入年功工资!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        gradedWages: [
            { required: true, message: '请输入等级工资!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        advancePerformance: [
            { required: true, message: '请输入预发绩效!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        executiveFund: [
            { required: true, message: '请输入执业金!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        reportApplicationFee: [
            { required: true, message: '请输入报告审核费!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        projectPerformance: [
            { required: true, message: '请输入项目绩效!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        settledThePreviousPeriod: [
            { required: true, message: '请输入前期已结算!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        other: [
            { required: true, message: '请输入其它!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        absenceDeduction: [
            { required: true, message: '请输入缺勤扣款!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        otherDeductions: [
            { required: true, message: '请输入其他扣款!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        // grossPay: [
        //     { required: true, message: '请输入应发工资!'},
        //     { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        // ],
        annualCumulativeIncome: [
            { required: true, message: '请输入'+ new Date().getFullYear() + '年累计收入!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        insuranceForNextMonth: [
            { required: true, message: '请输入下月社保!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        monthBond: [
            { required: true, message: '请输入当月公积金!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        yearFiveInsurancesCount: [
            { required: true, message: '请输入'+ new Date().getFullYear() + '年五险一金累计扣款!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        yearAdvanceCount: [
            { required: true, message: '请输入'+ new Date().getFullYear() + '年应预缴个人所得税!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        yearPaidIndividualCount: [
            { required: true, message: '请输入'+ new Date().getFullYear() + '年已预缴个人所得税金额!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        currentDuePersonal: [
            { required: true, message: '请输入本期应预缴个人所得税!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        yearSpecialCount: [
            { required: true, message: '请输入'+ new Date().getFullYear() + '年累计个税专项附加扣除金额!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        previouslyIssued: [
            { required: true, message: '请输入前期已发放!'},
            { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        // netPayment: [
        //     { required: true, message: '请输入实发工资!'},
        //     { pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        // ],
        },
        url: {
          add: "/api/erp/hr/payment/erpPayrollManagement/add",
          edit: "/api/erp/hr/payment/erpPayrollManagement/edit",
          queryById: "/api/erp/hr/payment/erpPayrollManagement/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      //更新工资信息
      inputAmount(){
        let basePay=this.model.basePay?this.model.basePay:0;
        let seniorityWage=this.model.seniorityWage?this.model.seniorityWage:0;
        let gradedWages=this.model.gradedWages?this.model.gradedWages:0;
        let advancePerformance=this.model.advancePerformance?this.model.advancePerformance:0;
        let executiveFund=this.model.executiveFund?this.model.executiveFund:0;
        let reportApplicationFee=this.model.reportApplicationFee?this.model.reportApplicationFee:0;
        let projectPerformance=this.model.projectPerformance?this.model.projectPerformance:0;
        let other=this.model.other?this.model.other:0;
        let absenceDeduction=this.model.absenceDeduction?this.model.absenceDeduction:0;
        let otherDeductions=this.model.otherDeductions?this.model.otherDeductions:0;
        this.model.grossPay=(basePay+seniorityWage+gradedWages+advancePerformance+executiveFund+reportApplicationFee+projectPerformance+other-absenceDeduction-otherDeductions).toFixed(2);
        let annualCumulativeIncome=this.model.annualCumulativeIncome?this.model.annualCumulativeIncome:0;
        let insuranceForNextMonth=this.model.insuranceForNextMonth?this.model.insuranceForNextMonth:0;
        let yearFiveInsurancesCount=this.model.yearFiveInsurancesCount?this.model.yearFiveInsurancesCount:0;
        let monthBond=this.model.monthBond?this.model.monthBond:0;
        let yearAdvanceCount=this.model.yearAdvanceCount?this.model.yearAdvanceCount:0;
        let yearPaidIndividualCount=this.model.yearPaidIndividualCount?this.model.yearPaidIndividualCount:0;
        let currentDuePersonal=this.model.currentDuePersonal?this.model.currentDuePersonal:0;
        let yearSpecialCount=this.model.yearSpecialCount?this.model.yearSpecialCount:0;
        let previouslyIssued=this.model.previouslyIssued?this.model.previouslyIssued:0;
        this.model.netPayment=(this.model.grossPay-insuranceForNextMonth-monthBond-currentDuePersonal).toFixed(2);
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>
